package tom.client;

import tom.shared.Product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class Library {
	public static final String USER_LEVEL[] = { "0 อ่านอย่างเดียว",
			"1 ใช้งานปกติ", "2 เปลี่ยนส่วนลด 1", "3 เปลี่ยนสวนลด 2 3 4",
			"4 พิมพ์เอกสารซ้ำ", "5 ถอยบิลอย่างเดียว", "6 ใช้งานปกติ/พิมพ์ซ้ำ",
			"7 เปิดส่วนลด/พิมพ์ซ้ำ", "8 แก้ไขทุกอย่าง", "9  ผู้ดูแลระบบ", };

	public enum FormMode {
		Clear, Open, Edit, Insert
	};

	public static FormMode checkPermission(FormMode mode) {
		FormMode m = mode;
		int level = FormLogin.currUser.getLevel();
		if (level == 0) {
			if (mode == FormMode.Edit || mode == FormMode.Insert) {
				m = FormMode.Open;
				Window.alert(USER_LEVEL[level]);
			}
		} else if (level == 1) {

		} else if (level == 2) {

		} else if (level == 3) {

		} else if (level == 4) {
			if (mode == FormMode.Edit || mode == FormMode.Insert) {
				m = FormMode.Open;
				Window.alert(USER_LEVEL[level]);
			}	
		} else if (level == 5) {
			if (mode == FormMode.Edit || mode == FormMode.Insert) {
				m = FormMode.Open;
				Window.alert(USER_LEVEL[level]);
			}	
		} else if (level == 6) {

		} else if (level == 9) {

		}

		return m;
	}

	public static final KeyDownEvent KEY_ENTER_EVENT = new KeyDownEvent() {
		public int getNativeKeyCode() {
			return KeyCodes.KEY_ENTER;
		}
	};

	public static final String ERROR_MESSAGE = "มีปัญหา การติดต่อกับฐานข้อมูล";

	public static final DateTimeFormat DATETIME_FORMAT = DateTimeFormat
			.getFormat("dd/MM/yyyy HH:mm:ss");

	public static final DateTimeFormat DATE_FORMAT = DateTimeFormat
			.getFormat("dd/MM/yyyy");

	public static final DateTimeFormat DBDATE_FORMAT = DateTimeFormat
			.getFormat("yyyy-MM-dd");

	public static final DateTimeFormat TIME_FORMAT = DateTimeFormat
			.getFormat("HH:mm:ss");

	private static ServiceAsync service;

	public static ServiceAsync getService() {
		if (service == null) {
			String url = GWT.getHostPageBaseURL() + "Service";
			service = GWT.create(Service.class);
			((ServiceDefTarget) service).setServiceEntryPoint(url);
		}
		return service;
	}

	private static tom.client.admin.ServiceAsync adminService;

	public static tom.client.admin.ServiceAsync getAdminService() {
		if (adminService == null) {
			String url = GWT.getHostPageBaseURL() + "AdminService";
			adminService = GWT.create(tom.client.admin.Service.class);
			((ServiceDefTarget) adminService).setServiceEntryPoint(url);
		}
		return adminService;
	}

	private static tom.client.invoice.ServiceAsync saleService;

	public static tom.client.invoice.ServiceAsync getSaleService() {
		if (saleService == null) {
			String url = GWT.getHostPageBaseURL() + "SaleService";
			saleService = GWT.create(tom.client.invoice.Service.class);
			((ServiceDefTarget) saleService).setServiceEntryPoint(url);
		}
		return saleService;
	}

	private static tom.client.account.ServiceAsync accService;

	public static tom.client.account.ServiceAsync getAccService() {
		if (accService == null) {
			String url = GWT.getHostPageBaseURL() + "AccService";
			accService = GWT.create(tom.client.account.Service.class);
			((ServiceDefTarget) accService).setServiceEntryPoint(url);
		}
		return accService;
	}

	private static tom.client.stock.ServiceAsync stkService;

	public static tom.client.stock.ServiceAsync getStkService() {
		if (stkService == null) {
			String url = GWT.getHostPageBaseURL() + "StkService";
			stkService = GWT.create(tom.client.stock.Service.class);
			((ServiceDefTarget) stkService).setServiceEntryPoint(url);
		}
		return stkService;
	}

	public static String encode(String plain) {
		String cipher = null;
		if (plain != null) {
			final int n = 16;
			final int l = plain.length();
			// final int t = n - l + 1;
			int i = 0;
			byte arr[] = new byte[n];
			while (i < n) {
				// for (int j = 0; j < l; j++)
				arr[i] = (byte) plain.charAt(i % l);
				i++;
			}
			for (i = 0; i < n; i++)
				arr[i] ^= arr[n - 1 - i] ^ l;
			cipher = toHex(arr);
		}
		return cipher;
	}

	private static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F' };

	private static String toHex(byte[] arr) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			byte b1 = (byte) (arr[i] / 16);
			byte b2 = (byte) (arr[i] % 16);
			sb.append(hex[b1]);
			sb.append(hex[b2]);
		}
		return sb.toString();
	}

	public static String concat(String s1, String s2) {
		StringBuilder sb = new StringBuilder();
		if (s1 != null)
			sb.append(s1);
		if (s1 != null && s2 != null)
			sb.append('/');
		if (s2 != null)
			sb.append(s2);
		return sb.toString();
	}

	public static String moneyFormat(Double value) {
		StringBuilder sb = new StringBuilder();
		if (value != null) {
			value *= 100;
			value = Math.round(value) / 100.0;

			if (value != null) {
				String s = value.toString();
				int l = s.length() - 1;
				int dot = s.indexOf(".");
				if (dot == -1) {
					sb.insert(0, ".00");
				} else {
					if ((s.length() - dot) == 2)
						sb.insert(0, '0');
					while (s.charAt(l) != '.') {
						sb.insert(0, s.charAt(l));
						l--;
					}
					sb.insert(0, s.charAt(l));
					l--;
				}
				int i = 0;

				while (l >= 0) {
					if (i > 0 && (i % 3 == 0) && s.charAt(l) != '-')
						sb.insert(0, ',');
					i++;
					sb.insert(0, s.charAt(l));
					l--;
				}
			}
		}
		return sb.toString();
	}

	private static String[] moneyUnit = { "", "สิบ", "ร้อย", "พัน", "หมื่น",
			"แสน", "ล้าน" };
	private static String[] moneyDigit = { "", "หนึ่ง", "สอง", "สาม", "สี่",
			"ห้า", "หก", "เจ็ด", "แปด", "เก้า" };

	public static String moneyText(Double value) {
		StringBuilder sb = new StringBuilder();
		if (value != null) {
			String s = String.valueOf(Math.round(value * 100));
			int i = 0;
			boolean satang = false;
			boolean baht = false;
			boolean zero = true;
			for (int j = s.length() - 1; j >= 0; j--) {
				byte x = (byte) (s.charAt(j) - '0');
				if (i == 0 && satang && !baht) {
					sb.insert(0, "บาท");
					baht = true;
				}
				if (x > 0 || i == 6) {
					sb.insert(0, moneyUnit[i]);
					if (i == 1 && x == 2)
						sb.insert(0, "ยี่");
					else if (i == 1 && x == 1)
						; // หนึ่งสิบ ไม่มี
					else
						sb.insert(0, moneyDigit[x]);
					zero = false;
				}
				if (i == 6)
					i = 1;
				else if (i == 1 && !satang) {
					i = 0;
					if (!zero)
						sb.append("สตางค์");
					else
						sb.append("ถ้วน");
					satang = true;
				} else
					i++;
			}
		}
		return sb.toString();
	}

	public static double discountSeries(Product product, double level) {
		double x = 100.0;
		if (level <= 6) {
			if (level < 1) {
				x = 0;
			} else {
				Double[] discount = new Double[6];
				discount[0] = product.discount;
				discount[1] = product.dc1;
				discount[2] = product.dc2;
				discount[3] = product.dc3;
				discount[4] = product.dc4;
				discount[5] = product.dc5;
				for (int i = 0; i < level; i++) {
					x = x * (100 - discount[i]) / 100.0;
					// Window.alert(i + " : " + discount[i] + ":" + x);
				}
				x = 100 - x;
				x *= 100000;
				x = Math.round(x) / 100000.0;
			}
		} else {
			x -= level;
		}
		return x;
	}

	public static int compare(Double x, Double y) {
		int d = 0;
		d = (int) Math.round(x * 100) - (int) Math.round(y * 100);
		return d;
	}
}
